/**********************************************************

@description:
*File Name:rename.c
*Author: ydx
*mail: yudongxiang93@163.com
*Create Time: Sat 10 May 2025 07:50:44 AM CST
**********************************************************/
#if 1
#include<stdio.h>
#include<fcntl.h>
#include<sys/types.h>
#include<string.h>
#include<dirent.h>
#include<stdlib.h>

//DIR *opendir(const char* name);
//struct dirent *readdir(DIR *dirp);
//int closedir(DIR *dirp)
int len(char *name){
	int i=0;
	while(*(name+i)!='\0')
		i++;
	return i;	
}
char upper_lower(char ch){
	if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
		return ch^32;
}
void test(){
	char* name="hello";
	char* res=strchr(name,'h');
	int ret=res-name;
	printf("%d\n",ret);
}
int main(){
#if 0
	test();
	printf("%d\n",len("helloworld"));
#endif
	char* content_1="#ifndef __";
	char* content_3="_H";
	char* content_4="#define __";
	DIR *dir=opendir("./");
	struct dirent *entry;
	while((entry=readdir(dir))!=NULL){
		if(strstr(entry->d_name,".h") && strchr(entry->d_name,'r')-entry->d_name!=0){
			printf("%s\n",entry->d_name);
			//get filename
			char* filename=(char*)malloc(sizeof(char)*len(entry->d_name));
			memset(filename,0x0,len(entry->d_name)-3);
			strncpy(filename,entry->d_name+3,len(entry->d_name)-3);
			//filename[len(entry->d_name)-1]='h';
			printf("%s,",filename);
			//write into the file head info
#if 0
			char* name_to_h=(char*)malloc(sizeof(char)*(len(filename)-2));
			strncpy(name_to_h,filename,len(filename)-2);
			for(int i=0;i<len(filename)-2;i++)
				name_to_h[i]=upper_lower(name_to_h[i]);
			printf("%s,",name_to_h);

			char* content=(char*)malloc(sizeof(char)*256);
			memset(content,0x0,256);
			strcat(content,content_1);
			strcat(content,name_to_h);
			strcat(content,content_3);
			strcat(content,"\n");
			strcat(content,content_4);
			strcat(content,name_to_h);
			strcat(content,content_3);
			strcat(content,"\n\n#endif\n\n");
			printf("%s\n",content);
			FILE* f=fopen(entry->d_name,"w");
			fwrite(content,1,len(content),f);
			fclose(f);
			rename(entry->d_name,filename);
#endif
			rename(entry->d_name,filename);
			free(filename);
			//free(name_to_h);
			//free(content);
		}
	}
	closedir(dir);
}
#endif
